import universalBehavior from '../../../behavior/universalBehavior'
import area from "../../../utils/area";
import dayjs from '../../../utils/day'
import { usePageRequest, useRequest } from "../../../utils/request";
import map from "../../../utils/map";

Component({
    behaviors: [universalBehavior],
    data: {
        areaList: area,
        // 是否显示悬浮搜索栏
        showSearchBar: false,
        // 是否显示时间选择
        showDateChange: false,
        // 是否显示城市选择
        showCityChange: false,
        // 省市
        province: '北京市',
        city: '北京市',
        // 日期
        date: [],
        dateString: [],
        dateNum: 1,
        // 搜索内容
        searchValue: '',
        // 列表选择的省份
        changeProvince: '',
    },
    observers: {
        'provinceList': function (data) {
            if (data && data.length) {
                this.setData({
                    changeProvince: data[0]
                })
            }
        },
        'changeProvince': function (data) {
            this.houseRequest.reset({
                province: data
            })
        }
    },
    methods: {
        onLoad() {
            // 日期初始化
            const date = [
                dayjs().hour(0).minute(0).second(0),
                dayjs().hour(0).minute(0).second(0).add(1, 'day')
            ]
            this.setData({
                date,
                dateString: date.map(item => item.format('MM月DD日'))
            })

            // 初始化省份城市
            this.provinceRequest = new useRequest('api/provincelist', {
                data: 'provinceList',
            }, this)
            this.houseRequest = new usePageRequest('api/provincehouselist', {
                hold: true,
                data: 'houseList',
                loading: 'houseLoading'
            }, this)

            // 初始化banner
            this.bannerRequest = new useRequest('api/gethomepagebannerimgs', {
                data: 'banner'
            }, this)
        },
        onShow() {
            this.getTabBar().init();
        },
        onPageScroll(e) {
            if (e.scrollTop > 320 && !this.data.showSearchBar) {
                this.setData({
                    showSearchBar: true
                })
            } else if (e.scrollTop < 320 && this.data.showSearchBar) {
                this.setData({
                    showSearchBar: false
                })
            }
        },
        async onPullDownRefresh() {
            this.setData({
                provinceList: null,
                houseList: null
            })
            await this.provinceRequest.run()
            wx.stopPullDownRefresh()
        },
        // 日期
        showDate() {
            this.setData({
                showDateChange: true
            })
        },
        onDateClose() {
            this.setData({
                showDateChange: false
            })
        },
        onDateChange(e) {
            this.onDateClose()
            const date = [dayjs(e.detail[0]), dayjs(e.detail[1])]
            this.setData({
                date,
                dateString: date.map(item => item.format('MM月DD日')),
                dateNum: date[1].diff(date[0], 'day')
            })
        },
        // 选择城市
        showCity() {
            this.setData({
                showCityChange: true
            })
        },
        hideCity() {
            this.setData({
                showCityChange: false
            })
        },
        changeCity(e) {
            this.hideCity()
            const data = e.detail.values
            this.setData({
                province: data[0].name,
                city: data[1].name
            })
        },
        // 搜索
        search() {
            wx.navigateTo({
                url: `/pages/search/index/index?city=${this.data.city}&start=${this.data.date[0].unix()}&end=${this.data.date[1].unix()}&value=${this.data.searchValue}`
            })
        },
        // 选择侧边
        changeProvince(e) {
            this.setData({
                changeProvince: e.currentTarget.dataset.title
            })
        },
        getLocation() {
            wx.getSetting({
                success: result => {
                    if (!result.authSetting.hasOwnProperty('scope.userLocation') || result.authSetting["scope.userLocation"]) {
                        map.reverseGeocoder({
                            success: (res) => {
                                this.setData({
                                    province: res.result.address_component.province,
                                    city: res.result.address_component.city,
                                })
                            }
                        })
                    } else {
                        wx.showModal({
                            title: '提示',
                            content: '无法获取您的位置，点击确认以打开授权设置页面',
                            success: result => {
                                if (result.confirm) {
                                    wx.openSetting()
                                }
                            }
                        })
                    }
                }
            })
        },
        // 分享
        onShareAppMessage() {
            return {
                title: '拥有一房，换游天下'
            }
        }
    }
})
